package com.ctshk.rpc.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.system.dto.MainDataFieldDTO;
import com.ctshk.rpc.system.entity.MainDataField;
import com.ctshk.rpc.system.mapper.MainDataFieldMapper;
import com.ctshk.rpc.system.mapper.MainDataTableMapper;
import com.ctshk.rpc.system.req.MainDataFieldReq;
import com.ctshk.rpc.system.req.MainDataTableFieldReq;
import com.ctshk.rpc.system.service.IMainDataFieldService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 基础数据字段表 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2020-12-29
 */
@DubboService
public class MainDataFieldServiceImpl extends ServiceImpl<MainDataFieldMapper, MainDataField> implements IMainDataFieldService {


    /**
     * 主数据表名称
     */
    @Autowired
    private MainDataTableMapper mainDataTableMapper;

    @Override
    public List<MainDataFieldDTO> queryList(Long tableId) {

        List<MainDataField> mainDataFieldList = baseMapper.selectList(new QueryWrapper<MainDataField>().lambda().eq(MainDataField::getTableId, tableId).eq(MainDataField::getIsDeleted, 0));
        return EntityUtil.copyList(mainDataFieldList, MainDataFieldDTO.class);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(MainDataTableFieldReq mainDataTableFieldReq, Long userId) {

        long tableId = mainDataTableFieldReq.getTableId();
        List<MainDataFieldReq> list = mainDataTableFieldReq.getList();
        List<MainDataField> mainDataFieldList = new ArrayList<>();
        MainDataField mainDataField = null;
        for (MainDataFieldReq mainDataFieldReq : list) {
            mainDataField = new MainDataField();
            BeanUtils.copyProperties(mainDataFieldReq, mainDataField);
            mainDataField.setCreateId(userId);
            mainDataField.setTableId(tableId);
            mainDataField.setIsDeleted(0);
            mainDataField.setIsFixed(0);
            mainDataFieldList.add(mainDataField);
        }
        //固定字段
        for (int i = 0; i < 3; i++) {
            MainDataField dataField = new MainDataField();
            dataField.setTableId(tableId);
            dataField.setCreateId(userId);
            dataField.setIsFixed(1);
            dataField.setIsDeleted(0);
            switch (i) {
                case 0:
                    dataField.setFieldName("修改人");
                    break;
                case 1:
                    dataField.setFieldName("修改时间");
                    break;
                case 2:
                    dataField.setFieldName("系统编码");
                    dataField.setIsDeleted(1);
                    break;
                default:
                    break;
            }
            mainDataFieldList.add(dataField);
        }

        super.saveBatch(mainDataFieldList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateField(MainDataTableFieldReq mainDataTableFieldReq, Long userId) {
        Long tableId = mainDataTableFieldReq.getTableId();
        if (null == tableId) {
            throw new BusinessException(SystemError.SYS_402);
        }

        List<MainDataFieldReq> mainDataFieldReqList = mainDataTableFieldReq.getList();

        if (CollectionUtils.isEmpty(mainDataFieldReqList)) {
            throw new BusinessException(SystemError.SYS_402);
        }

        List<MainDataField> newFieldList = EntityUtil.copyList(mainDataFieldReqList, MainDataField.class);
        List<MainDataField> oldFieldList = baseMapper.selectList(new QueryWrapper<MainDataField>().lambda().eq(MainDataField::getTableId, tableId));

        List<MainDataField> addMainDataField = new ArrayList<>();

        HashMap<Long, MainDataField> newMap = new HashMap<>(newFieldList.size());
        for (MainDataField mainDataField : newFieldList) {
            if (mainDataField.getId() != null) {
                newMap.put(mainDataField.getId(), mainDataField);
            } else {
                mainDataField.setTableId(tableId);
                mainDataField.setIsFixed(0);
                addMainDataField.add(mainDataField);
            }
        }

        List<MainDataField> updateMainDataField = new ArrayList<>();
        for (MainDataField mainDataField : oldFieldList) {
            Long fieldId = mainDataField.getId();
            if (newMap.containsKey(fieldId)) {
                MainDataField newMainDataField = newMap.get(fieldId);
                if (null != newMainDataField) {
                    updateMainDataField.add(newMainDataField);
                    newMap.remove(fieldId);
                }
            } else {
                mainDataField.setIsDeleted(1);
                updateMainDataField.add(mainDataField);
            }
        }


        super.saveBatch(addMainDataField);
        super.updateBatchById(updateMainDataField);

    }
}
